﻿using System;
using System.Collections.Generic;
 
using System.Text;

namespace GGSharp.Packets
{
    /// <summary>
    /// Pakiet zawierajacy czesc lub calosc eksportowanej listy kontaktow
    /// </summary>
    public class UserListRequestPacket : HeaderPacket
    {
        protected byte _type;		/* rodzaj zapytania */
        protected string _request = "";		/* treść, nie musi wystąpić */

        public UserListRequestPacket()
        {
            this.Length = 1;
            this.Type = PacketType.GG_USERLIST_REQUEST;
        }

        public string Request
        {
            get { return _request; }
            set { _request = value; }
        }

        public UserListRequestType RequestType
        {
            get { return TranslateRequestType(_type); }
            set { _type = (byte)value; }
        }

        public byte RequestTypeRaw
        {
            get { return _type; }
            set { _type = value; }
        }

        protected UserListRequestType TranslateRequestType(byte type)
        {
            try
            {
                return (UserListRequestType)_type;
            }
            catch (Exception e)
            {
                return UserListRequestType.Unknown;
            }
        }

        public override void Write(System.IO.BinaryWriter writer)
        {
            if (RequestType != UserListRequestType.Get)
                this.Length += _request.Length + 1;

            base.Write(writer);

            writer.Write(_type);

            if (RequestType != UserListRequestType.Get)
            {
                writer.Write(Encoding.GetEncoding("windows-1250").GetBytes(_request));
                writer.Write('\0');
            }
        }
        
    }
}
